#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <queue>
vector<int> dx = { 1,-1,0,0 };
vector<int> dy = { 0,0,1,-1 };
int main() {
    int n, m;
    cin >> n >> m;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    vector<vector<char>> vv(n + 1, vector<char>(m + 1, '*'));
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cin >> vv[i][j];
    }
    queue<pair<int, int>> q;
    q.push(make_pair(x1, y1));
    vv[x1][y1] = '*';
    int step = 1;
    while (!q.empty())
    {
        int size = q.size();
        for (int i = 0; i < size; i++)
        {
            pair<int, int> tmp = q.front();
            q.pop();
            for (int k = 0; k < 4; k++)
            {
                int x = dx[k] + tmp.first;
                int y = dy[k] + tmp.second;
                if (x >= 1 && x <= n && y >= 1 && y <= m && vv[x][y] != '*')
                {
                    if (x == x2 && y == y2)
                    {
                        cout << step;
                        return 0;
                    }
                    q.push(make_pair(x, y));
                    vv[x][y] = '*';
                }
            }
        }
        step++;
    }
    cout << -1;
    return 0;
}